package com.dl.service;

import com.dl.dao.*;
import com.dl.pojo.*;
import com.dl.tool.Algo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CourseSetupService {

    @Autowired
    private CourseSetupMapper courseSetupMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CourseChooseMapper courseChooseMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    Random r = new Random();

    public List<CourseSetup> imitCourseSetup(){
        //清空开课表，删除学生选课表的开课班级的值。
        courseChooseMapper.eraseSetupid();
        courseSetupMapper.delAllCourseSetups();

        //遍历各科目，根据该科目的选课人数来制定学生数*开课数，并根据开课数来随机选取老师。
        List<Course> courses = courseMapper.selCourses(new Course());
        List<Teacher> teachers = teacherMapper.selTeachers(new Teacher());
        for (Course course:courses) {
            if(course.getCourse_roomtype()<0){continue;}

            CourseChoose courseChoose_temp = new CourseChoose();
            courseChoose_temp.setChoose_courseid(course.getCourse_id());
            List<CourseChoose> courseChooses = courseChooseMapper.selCourseChooses(courseChoose_temp);
            int choose_count = courseChooses.size();
            if(choose_count==0){continue;}

            Map<String, Integer> setPlan = Algo.getSetPlan(choose_count, 90);
            Map<String, Integer> planB = Algo.getSetPlan(choose_count, 300);
            if(setPlan.get("fitness") < planB.get("fitness")){
                setPlan = planB;
            }

            for(int i=0;i<setPlan.get("setNum");i++){
                CourseSetup courseSetup_temp = new CourseSetup();
                Teacher teacher_temp = teachers.get(r.nextInt(teachers.size()));
                courseSetup_temp.setSetup_teacherid(teacher_temp.getTeacher_id());
                courseSetup_temp.setSetup_courseid(course.getCourse_id());
                courseSetup_temp.setSetup_name(course.getCourse_name()+" - "+teacher_temp.getTeacher_name());
                if(i<setPlan.get("setNum")-1){
                    courseSetup_temp.setSetup_count(setPlan.get("stusPerSet"));
                }else {
                    courseSetup_temp.setSetup_count(setPlan.get("lastStus"));
                }
                courseSetupMapper.insertCourseSetup(courseSetup_temp);
            }

            //为该课程创建开课数据后，为学生选课表分配开课班级
            CourseSetup courseSetup_temp = new CourseSetup();
            courseSetup_temp.setSetup_courseid(course.getCourse_id());
            List<CourseSetup> courseSetups_temp = courseSetupMapper.selCourseSetups(courseSetup_temp);

            int index=0;
            for(CourseSetup courseSetup:courseSetups_temp){
                for(int i=0;i<courseSetup.getSetup_count();i++){
                    CourseChoose courseChoose_temp2 = courseChooses.get(index++);
                    courseChoose_temp2.setChoose_setupid(courseSetup.getSetup_id());
                    courseChooseMapper.updateSetupid(courseChoose_temp2);
                }
            }
        }
        return courseSetupMapper.selCourseSetups(new CourseSetup());
    }
}
